﻿Imports Trinkets.Core

Public Class DataGridManager

    Private Sub New()

    End Sub

    Public Shared Sub Update(ByVal grid As DataGrid, _
                             ByVal database As CsvDatabase, _
                             ByVal table As TableSchema, _
                             Optional ByVal showRecords As Boolean = True)

        ' Ensure the data grid is configured correctly
        ConfigureGrid(grid, table)

        If showRecords Then

            ' Get all the records and calculate computed fields
            Dim Records As BaseEntity() = database.Tables(table.Name).GetAllRecords
            For Each Record As BaseEntity In Records
                Record.CalculateComputedFields()
            Next

            ' Display the records in the grid
            grid.ItemsSource = Records

        End If

    End Sub

    Public Shared Sub Update(ByVal grid As DataGrid, _
                             ByVal database As CsvDatabase, _
                             ByVal table As TableSchema, _
                             ByVal columnName As String, _
                             ByVal fieldValue As String)

        ' Ensure the data grid is configured correctly
        ConfigureGrid(grid, table)

        ' Get all the records and calculate computed fields
        Dim Records As BaseEntity() = database.Tables(table.Name).FindRecords(columnName, fieldValue)
        For Each Record As BaseEntity In Records
            Record.CalculateComputedFields()
        Next

        ' Display the records in the grid
        grid.ItemsSource = Records

    End Sub

    Private Shared Sub ConfigureGrid(ByVal grid As DataGrid, ByVal table As TableSchema)

        ' Set up the main data grid properties
        grid.AutoGenerateColumns = False
        grid.HorizontalGridLinesBrush = grid.BorderBrush
        grid.VerticalGridLinesBrush = grid.BorderBrush
        grid.IsReadOnly = True

        ' Add display columns to the data grid
        grid.Columns.Clear()
        For Each Field As FieldSchema In table.AllFields
            If Field.IsDisplayColumn AndAlso Not Field.IsPrimaryKey Then
                Dim Column As DataGridColumn = CreateDataGridColumn(Field)
                grid.Columns.Add(Column)
            End If
        Next

    End Sub

    Private Shared Function CreateDataGridColumn(ByVal field As FieldSchema) As DataGridColumn

        Dim Result As DataGridColumn = Nothing

        If field.FieldType Is GetType(Boolean) Then

            ' Create a checkbox column
            Dim Column As New DataGridCheckBoxColumn
            Column.Binding = New Binding(field.Name)
            Result = Column

        Else

            ' Everything else is a text column
            Dim Column As New DataGridTextColumn
            Column.Binding = New Binding(field.Name)
            Result = Column

        End If

        ' Check that a column was assigned
        If Result Is Nothing Then Throw New Exception("Could not create a column for field '" + field.Name + "'.")

        ' Return the column
        Result.Header = field.DisplayName
        Return Result

    End Function

End Class
